/*
 * Copyright (C) 2010 GrayNM <gray-nm@mail.ru>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

#ifndef __STATSFILE_H__
#define __STATSFILE_H__

#include <stdio.h>
#include <time.h>
#include "../../../../text/include/ZLTextStats.h"

/// marker of file block
#define STATSFILE_MARKER			0x5453

/// current version of file structure
#define STATSFILE_CURRENT_VERSION	0x03

// file block types
enum ZLTextStatsFileBlocks {
	TSFB_Version=0,				///< version block
	TSFB_PageInfo,				///< page statistics information
	TSFB_Operation,				///< operation info
};

// common file block header
struct ZLTSFBlockHeader {
	ZLTSFBlockHeader(unsigned short _marker,unsigned char _type):marker(_marker),type(_type) {}
	
	unsigned short		marker;		// block marker \ref STATSFILE_MARKER
	unsigned char		type;		// block type \ref ZLTextStatsFileBlocks
};

// file version block
struct ZLTSFVersionBlock: public ZLTSFBlockHeader {
	ZLTSFVersionBlock(unsigned char _version,unsigned long long _hardware):
		ZLTSFBlockHeader(STATSFILE_MARKER,TSFB_Version),version(_version),clock_per_sec(CLOCKS_PER_SEC),hardware(_hardware) {}

	unsigned char		version;		// file structure version
	clock_t				clock_per_sec;	// CLOCKS_PER_SEC value
	unsigned long long	hardware;		// hardware config
};

// line statistics record
struct ZLTSFPageStatsLine1 {
	ZLTSFPageStatsLine1():WordCounter(0),MaxWordLength(0),ImageCounter(0),SpaceCounter(0),StyleWidthOfSpaces(0),RealWidthOfSpaces(0) {}
	ZLTSFPageStatsLine1(const ZLTextLineStats &ls):WordCounter((unsigned short)ls.WordCounter),
		MaxWordLength((unsigned short)ls.MaxWordLength),ImageCounter((unsigned short)ls.ImageCounter),SpaceCounter((unsigned short)ls.SpaceCounter),
		StyleWidthOfSpaces((unsigned short)ls.StyleWidthOfSpaces),RealWidthOfSpaces((unsigned short)ls.RealWidthOfSpaces) {}

	unsigned short	WordCounter;
	unsigned short	MaxWordLength;
	unsigned short	ImageCounter;
	unsigned short	SpaceCounter;
	unsigned short	StyleWidthOfSpaces;	// summary width of spaces according to style
	unsigned short	RealWidthOfSpaces;	// real width of empty space in line to extend spaces to it
};

struct ZLTSFPageStatsLine {
	ZLTSFPageStatsLine():WordCounter(0),MaxWordLength(0),ImageCounter(0),SpaceCounter(0),StyleWidthOfSpaces(0),RealWidthOfSpaces(0),SymbolCounter(0) {}
	ZLTSFPageStatsLine(const ZLTextLineStats &ls):WordCounter((unsigned short)ls.WordCounter),MaxWordLength((unsigned short)ls.MaxWordLength),
		ImageCounter((unsigned short)ls.ImageCounter),SpaceCounter((unsigned short)ls.SpaceCounter),StyleWidthOfSpaces((unsigned short)ls.StyleWidthOfSpaces),
		RealWidthOfSpaces((unsigned short)ls.RealWidthOfSpaces),SymbolCounter(ls.SymbolCounter) {}

	unsigned short	WordCounter;
	unsigned short	MaxWordLength;
	unsigned short	ImageCounter;
	unsigned short	SpaceCounter;
	unsigned short	StyleWidthOfSpaces;	// summary width of spaces according to style
	unsigned short	RealWidthOfSpaces;	// real width of empty space in line to extend spaces to it
	unsigned long	SymbolCounter;
};

// page statistics block
struct ZLTSFPageStatsBlock1: public ZLTSFBlockHeader {
	ZLTSFPageStatsBlock1(unsigned short _wc,unsigned short _ic,unsigned short _lc):
		ZLTSFBlockHeader(STATSFILE_MARKER,TSFB_PageInfo),ImageCounter(_ic),DisplayTime(clock()),WordCounter(_wc),LinesCount(_lc) {}
	
	unsigned char		ImageCounter;
	clock_t				DisplayTime;	// relative time of page display in clocks
	unsigned short		WordCounter;
	unsigned short		LinesCount;
	ZLTSFPageStatsLine1	Lines[];		// variable array of line infos
};

struct ZLTSFPageStatsBlock: public ZLTSFBlockHeader {
	ZLTSFPageStatsBlock(unsigned short _wc,unsigned short _ic,unsigned long _sc,unsigned short _lc):
		ZLTSFBlockHeader(STATSFILE_MARKER,TSFB_PageInfo),ImageCounter(_ic),DisplayTime(clock()),WordCounter(_wc),SymbolCounter(_sc),LinesCount(_lc) {}

	unsigned char		ImageCounter;
	clock_t				DisplayTime;	// relative time of page display in clocks
	unsigned short		WordCounter;
	unsigned long		SymbolCounter;
	unsigned short		LinesCount;
	ZLTSFPageStatsLine	Lines[];		// variable array of line infos
};

enum ZLStatsFileOperations {
	SFO_Dummy = 0,			// dummy operation
	SFO_NextPage,			// go to next page
	SFO_PrevPage,			// go to prev page
	SFO_OtherPage,			// go to other page
	SFO_Interface,			// some interface manipulations
	SFO_Exit,				// quit application
	SFO_LockOn,				// reader is locked
	SFO_LockOff,			// reader is unlocked
};

// operation info block
struct ZLTSFOperationBlock: public ZLTSFBlockHeader {
	ZLTSFOperationBlock(unsigned short _operation):
		ZLTSFBlockHeader(STATSFILE_MARKER,TSFB_Operation),Operation(_operation),OperationTime(clock()) {}

	unsigned char		Operation;		// operation code \ref ZLStatsFileOperations
	clock_t				OperationTime;	// relative time of operation in clocks
};

/// Class to append statistics file with new information
class ZLTextStatsFileWriter {
public:
	ZLTextStatsFileWriter(const char *filename);
	~ZLTextStatsFileWriter();

	void writePageStats(const ZLPageStats *pagestats);
	void writeOperation(ZLStatsFileOperations op);

protected:
	FILE	*f;
};

#endif /* __STATSFILE_H__ */
